#!/bin/bash

SERVICE_NAME=$1

# 先去找配置文件
running_path=$(pwd)
echo "当前运行脚本的目录: ${running_path}"
cd ../config || exit
config_json=$(cat blackbean-deploy-config.json)

# 获取服务配置
service_info_json=$(echo "${config_json}" | jq -r '.service_info')
service_length=$(echo "${service_info_json}" | jq 'length')
match_service_array=()
for ((i = 0; i < "${service_length}"; i++)); do
    service_info=$(echo "${service_info_json}" | jq ".[${i}]")
    name=$(echo "${service_info}" | jq -r ".name")
    if [[ ${name} =~ ${SERVICE_NAME} ]]; then
        match_service_array[${#match_service_array[@]}]=${service_info}
        break
    fi
done

if [ ${#match_service_array[@]} -eq 0 ]; then
    echo "服务名称${SERVICE_NAME}在配置中不存在！"
    exit
fi

match_service_name=$(echo "${match_service_array[0]}" | jq -r '.name')
echo "${match_service_name}"

start() {
    status
    if [ $? -eq 1 ]; then
        echo "服务${match_service_name}已经启动，PID=${SERVICE_PID}"
        exit
    else
        echo "输入的服务名称：${SERVICE_NAME} 完整名称: ${match_service_name}"

        jvm_opt=$(echo "${match_service_array[0]}" | jq -r '.jvm_opt')
        env=$(echo "${match_service_array[0]}" | jq -r '.env')
        service_port=$(echo "${match_service_array[0]}" | jq -r '.port')

        # 获取jar包存放路径
        jar_path=$(echo "${config_json}" | jq -r '.jar_path')

        if [ -z "$jar_path" ]; then
            cd "$running_path" || exit
            cd ../jars || exit
            jar_path=$(pwd)
            echo "未配置jar包存放路径，使用默认路径: $jar_path"
        else
            echo "jar包存放路径:$jar_path"
        fi

        # 查看jar包文件是否存在
        jar_file=${jar_path}/${match_service_name}.jar

        if [ ! -f "${jar_file}" ]; then
            echo "${jar_file}不存在"
            exit
        fi

        echo "找到jar包文件"
        jar_size=$(stat "${jar_file}" -c '%s')
        echo "  jar包名称：$(basename "${jar_file}")"
        echo "  jar包大小: $(echo | awk "{print int((${jar_size}/1024/1024)+0.5)}" )mb"

        jar_create_time=$(stat "${jar_file}" -c '%w')
        echo "  jar包创建时间:${jar_create_time} " "  当前时间: $(date +"%Y-%m-%d %H:%M:%S.%s %z")"

        # 获取日志文件输出路径
        log_path=$(echo "${config_json}" | jq -r '.log_path')
        if [ -z "$log_path" ]; then
            cd "$running_path" || exit
            cd ../logs || exit
            log_path=$(pwd)
        fi
        log_path=${log_path}/${match_service_name}/
        log_file=${log_path}${match_service_name}.log
        echo "日志路径: $log_path"
        echo "日志文件: $log_file"
        # 构建运行命令
        # start_script="nohup java ${jvm_opt} -jar ${jar_file} --spring.profiles.active=${env} --logging.file.path=${log_path} --logging.file.name=${log_file} >/dev/null 2>&1 &"
        # echo "${start_script}"
        # service_port=

        if [[ -n ${service_port} ]]; then
            service_port="--server.port="${service_port}
        fi

        service_env="--spring.profiles.active="${env}
        service_log_config="--logging.file.path=${log_path} --logging.file.name=${log_file}"

        #
        nohup java "${jvm_opt}" -jar "${jar_file}" "${service_env}" "${service_port}" "${service_log_config}" >/dev/null 2>&1 &
        exit 0
    fi
}

stop() {
    status
    if [ $? -eq 1 ]; then
        kill -9 "${SERVICE_PID}"
        echo "关闭服务进程..."
        sleep 2
        echo "关闭完成"
    fi
}

restart() {
    stop
    start
}

status() {
    SERVICE_PID=$(pgrep -a java | grep /home/ubuntu/workspace/blackbean/jars/ |  grep "${match_service_name}" |awk '{print $1}')
    if [ -n "$SERVICE_PID" ]; then
        echo "服务已启动，PID = $SERVICE_PID"
        return 1
    else
        echo "服务未启动"
        return 0
    fi
}

usage() {
    echo "Usage: bash [APP_NAME] [start|stop|restart|status]"
    exit 1
}

case $2 in
"start")
    start
    ;;
"stop")
    stop
    ;;
"restart")
    restart
    ;;
"status")
    status
    ;;
*)
    usage
    ;;
esac

exit 0
